﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TLSharp;

public partial class Ladder : System.Web.UI.Page
{
    public string CookieUserName = "";
    public string CookiePassword = "";
    public string AppGuid = System.Configuration.ConfigurationSettings.AppSettings["AppGuid"];
    int PageSize = 50;

    protected void Page_Load(object sender, EventArgs e)
    {
       
        CookieUserName = Request.Cookies["TLUsername"] != null ? Request.Cookies["TLUsername"].Value : "";
        CookiePassword = Request.Cookies["TLPassword"] != null ? Request.Cookies["TLPassword"].Value : "";
        double LadderStartRank = System.Convert.ToDouble(Request["LadderStartRank"]);
        int ThisTeamID = (Request["ThisTeamID"]!=null ? System.Convert.ToInt32(Request["ThisTeamID"] ): 0);
        bool bNewList = true;
        if(Request["bNewList"]!= null){ bNewList=false;}
        BrowseLadderResponse TurnReq = TouchLeagueService.BrowseLadder(CookieUserName, CookiePassword, AppGuid, LadderStartRank);
        
        System.Text.StringBuilder ResponseText = new System.Text.StringBuilder();
        if (bNewList)
        {
            ResponseText.Append("<ul id='Ladder' title='Ladder'>");
        }
        if (LadderStartRank != 1)
        {
            double NewLadderStartRank = (TurnReq.LadderObjects.First().CurrentRank - PageSize);
            if (NewLadderStartRank < 1)
            {
                NewLadderStartRank = 1;
            }
            ResponseText.Append("<li><a href='Ladder.aspx?ThisTeamID=" + ThisTeamID.ToString() + "&bNewList=false&StartRank=" + NewLadderStartRank.ToString() + "' target=_replace>More..</a></li>");

        }
        foreach (Team ThisTeam in TurnReq.LadderObjects)
        {
            if (ThisTeam.TeamID != ThisTeamID)
            {
                ResponseText.Append("<li><a href='ChallengeProcess.aspx?TargetTeamID=" + ThisTeam.TeamID.ToString() + "&bChallenge=on'>#" + ThisTeam.CurrentRank.ToString() + " - " + ThisTeam.TeamGeo + " " + ThisTeam.TeamMascot + "<br/>(" + ThisTeam.AllTimeWins.ToString() + "-" + ThisTeam.AllTimeLosses.ToString() + "-" + ThisTeam.AllTimeForfeit.ToString() + ")<br/>Challenge Them!</a></li>");
            }
            else
            {
                ResponseText.Append("<li style='background-color:#86B4E5;'>#" + ThisTeam.CurrentRank.ToString() + " - " + ThisTeam.TeamGeo + " " + ThisTeam.TeamMascot + "<br/>(" + ThisTeam.AllTimeWins.ToString() + "-" + ThisTeam.AllTimeLosses.ToString() + "-" + ThisTeam.AllTimeForfeit.ToString() + ")</li>");
            }
        }
        if (TurnReq.LadderObjects.Count == PageSize)
        {
            ResponseText.Append("<li><a href='Ladder.aspx?ThisTeamID=" + ThisTeamID.ToString() + "&bNewList=false&LadderStartRank=" + (TurnReq.LadderObjects.Last().CurrentRank + .0001).ToString() + "' target=_replace>More..</a></li>");
        }

        Response.Write(ResponseText.ToString());
        Response.End();


    }
}
